package com.donleo.stmg.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;

/**
 * @author liangd
 * date 2020-12-21 14:58
 * code 时间转换工具类
 */
public class DateUtil {
    /**
     * 获取当前时间
     *
     * @return String
     */
    public static String getCurrentTime() {
        Date date = new Date();
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 替换T
     *
     * @param str
     * @return
     */
    public static String getDate(String str) {
        return str.replace("T", " ");
    }

    /**
     * 获取当天时间
     *
     * @return
     */
    public static String getCurrentDay() {
        Date date = new Date();
        // 获取日历
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 在当前日基础上+1，前一天-1，当天0
        calendar.add(Calendar.DAY_OF_MONTH, 0);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        return format.format(calendar.getTime());
    }

    /**
     * 获取当天下一天
     *
     * @return
     */
    public static String getNextDay() {
        Date date = new Date();
        // 获取日历
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 在当前日基础上+1，前一天-1，当天0
        calendar.add(Calendar.DAY_OF_MONTH, +1);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        return format.format(calendar.getTime());
    }


    /**
     * 获取当前时间
     *
     * @return String yyyy-MM-dd-HHmmss
     */
    public static String getWordCurrent() {
        Date date = new Date();
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HHmmss");
        return sdf.format(date);
    }

    /**
     * Date转String
     *
     * @param date Date
     * @return String
     */
    public static String getStringDate(Date date) {
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    /**
     * +8小时，用于存储es Date类型数据
     *
     * @param t1
     * @return
     */
    public static Date addTime(Date t1) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(t1);
        cal.add(Calendar.HOUR_OF_DAY, +8);
        return cal.getTime();
    }
}
